import $service from '../../../service.js';
var that;
const app = getApp();

let Timer = null
let t = 60

Page({
    data:{
        code: '',
        codeCentent: '获取验证码',
        isshow:false,
        isshow2:false,
        isshow3:true,
        password:'',
        password2:'',
        phone:'',
        imgCode:'',
        cToken: '',
        pageType: '',
        isagreen: false,
        loginType: 'P',
        isSend: false,
        loginTypeList: [{
            text: '用户',
            type: 'P',
        },{
            text: '医生',
            type: "D",
        }],
    },
    onLoad(e) {
        if (e.type === 'forget') {
            wx.setNavigationBarTitle({title: '忘记密码'})
        } else {
            wx.setNavigationBarTitle({title: '用户注册'})
        }
        this.setData({
            pageType: e.type
        })
    },
    onShow(){
        that = this.data;
    },
    sendcode(e){
        if (!this.data.isSend) {
          if ($service.isUndefinedAndEmpty(that.phone)) {
              $service.showToast("请输入手机号");
              return;
          }
          if (!$service.validateElement('mobile', that.phone)) {
              $service.showToast('请输入正确的手机号！');
              return;
          }
          this.captcha()
        } else {
          $service.showToast(`请在${this.data.codeCentent}重新获取`)
        }
    },
    captcha() {
      // app.http.captcha().then(res => {
      //   this.setData({
      //     imgCode: 'data:image/png;base64,'+res.data.img,
      //     cToken: res.data.cToken, 
      //     isshow3:false
      //   }) 
      // }).catch(err => {
      //   $service.showToast(err.msg);
      // })
      let parmas = {
        "mobile": that.phone,
        "type": that.pageType == 'forget' ?  that.loginType == 'P' ? 1 : 3 : 1,
        "isRegister": that.pageType == 'forget' ? 0 : 1
      }
      app.http.phoneCaptcha(parmas).then(res => {
          $service.showToast(res.msg);
          this.setData({
              isSend: true,
          })
          this.count()
      }).catch(err => {
          $service.showToast(err.msg);
      })
    },
  // 计时器
  count() {
      clearInterval(Timer)
      Timer = setInterval(() => {
          if (t == 1) {
              this.setData({
                  isSend: false,
                  codeCentent: '获取验证码'
              })
              t = 60
              this.setData({
                codeCentent: `${t}秒后重新获取`
              })
              clearInterval(Timer)
              return
          }
          t--;
          this.setData({
            codeCentent: `${t}秒后重新获取`
          })
      }, 1000);
  },

   
    inputChange(e){
        var name = e.detail.value;
        var index = e.currentTarget.dataset.index;
        if(index == 1){
            that.phone = name;
        }else if(index == 2){
            that.code = name;
        }else if(index == 3){
            that.password = name.replace(/[^\w\`\~\!\@\#\$\%\^\&\*\(\)\_\+\[\]\;\'\,\.\/\<\>\?\-\=\\]/ig,'');;
        }else if(index == 4){
            that.password2 = name.replace(/[^\w\`\~\!\@\#\$\%\^\&\*\(\)\_\+\[\]\;\'\,\.\/\<\>\?\-\=\\]/ig,'');;
        }
        this.setData({
            phone:that.phone,
            code:that.code,
            password: that.password,
            password2:that.password2
        })
    },
    tapisshow(e){
        var index = e.currentTarget.dataset.index;
        if(index == 1){
            this.data.isshow = !this.data.isshow;
        }else{
            this.data.isshow2 = !this.data.isshow2;
        }
        
        this.setData({
            isshow : this.data.isshow,
            isshow2 : this.data.isshow2
        })
    },
    tohome(){
        if ($service.isUndefinedAndEmpty(that.phone)) {
            $service.showToast("请输入手机号");
            return;
        }
        if (!$service.validateElement('mobile', that.phone)) {
            $service.showToast('请输入正确的手机号！');
            return;
        }
        // if ($service.isUndefinedAndEmpty(that.cToken)) {
        //     $service.showToast('请先获取验证码！');
        //     return;
        // }
        if ($service.isUndefinedAndEmpty(that.code)) {
            $service.showToast('请输入验证码！');
            return;
        }
        if ($service.isUndefinedAndEmpty(that.password) || $service.isUndefinedAndEmpty(that.password2) ) {
            $service.showToast('请输入密码！');
            return;
        }
        if (that.password.trim().length < 6 || that.password.trim().length > 20) {
          $service.showToast('请输入6-20位密码！');
          return;
        }
        if (that.password != that.password2 ) {
            $service.showToast('两次密码不一致');
            return;
        }

        if (!that.isagreen && this.data.pageType != 'forget') {
          $service.showToast('请查看并同意《服务协议与隐私政策》！')
        }
        var params = {
            "phoneCaptcha": that.code,
            "confirmPassword": that.password2,
            "mobile": that.phone,
            "password": that.password,
            "userType": that.loginType
        };
        if (that.pageType === 'forget') {
            app.http.retrievePassword(params).then(res => {
                $service.showToast(res.msg);
                let Timer = setTimeout(() => {
                    clearTimeout(Timer)
                    wx.reLaunch({
                        url:'../login'
                    })
                },1000)
            }).catch(err => {
                $service.showToast(err.msg);
            })
        } else {
            app.http.userRegister(params).then(res => {
                $service.showToast(res.msg);
                let Timer = setTimeout(() => {
                    clearTimeout(Timer)
                    wx.reLaunch({
                        url:'../login'
                    })
                },1000)
            }).catch(err => {
                $service.showToast(err.msg);
            })
        }
    },
    // 选择登陆方式
    checkType(e) {
        let {item} = e.currentTarget.dataset
        this.setData({
            "loginType": item.type,
        })
    },
     // 勾选协议
     choosetap() {
      this.data.isagreen = !this.data.isagreen
      this.setData({
        isagreen: this.data.isagreen
      })
    },
    goToAgeen() {
      $service.openPage(`/pages/login/agreement/agreement?type=2`)
    }
})